<?php
class Controller_Node extends FLEA_Controller_Action {
    private $modelNode = NULL;
    private $viewSimple = NULL;
    function Controller_Node() {
        $this->modelNode = FLEA::getSingleton('Model_Node');
    }
    function actionIndex() {
        $this->_forward('Node', 'NodeList');
    }
    function actionView($nid = NULL) {
        if(!isset($_GET['nid'])) {
            $this->_forward('Node', 'NodeList');
        }
        $nid = isset($_GET['nid'])?$_GET['nid']:1;
        $node = $this->modelNode->view($nid);
        $theme = FLEA::getSingleton('Theme_Node');
        $content = $theme->themeNodeView(array('node' => $node));
        $theme->themePage(array('content' => $content));
    }
    function actionNew() {
        $_POST['title'] = isset($_POST['title'])? $_POST['title']: NULL;
        if(!$_POST['title']) {
            $theme = FLEA::getSingleton('Theme_Node');
            $content = $theme->themeAddNode(array('action' => url('Node', 'New')));
            $theme->themePage(array('content' => $content));
        } else {
            $node = array(
                'title' => $_POST['title'],
                'body' => $_POST['body'],
                'publish' => 1,
                'author' => 1, // 发表者的uid
            );
            $nid = $this->modelNode->create($node);
            redirect(url('Node', 'View', array('nid' => $nid)));
        }
    }
    function actionNodeList() {
        // 查找所有发表的内容节点
        $nodes = $this->modelNode->findAllByField('publish', 1);
        $list = array();
        $theme = FLEA::getSingleton('Theme_Node');
        foreach($nodes as $node) {
            $node['body'] = mb_substr($node['body'], 0, 255);
            array_push($list, $theme->themeNodeListItem(array('node' => $node)));
        }
        $content = $theme->themeNodeList(array('nodeList' => $list));
        $theme->themePage(array('content' => $content));
    }
}
